﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using ServerPattern;
using HTA.Bluetooth;
using System.Xml.Serialization;
using RobotCtrl;

namespace CommandServer
{
    public class CommandHandler : AbstractBTHandler
    {
        private StreamReader sr;
        private StreamWriter sw;
        private NetworkStream nws;
        private Robot robot;

        public CommandHandler(BluetoothClient client, Robot r)
            : base(client)
        {
            Console.WriteLine("Incoming connection " + client.GetSocket().RemoteEndPoint);
            nws = client.GetStream();
            sr = new StreamReader(nws);
            sw = new StreamWriter(nws);
            this.robot = r;
        }

        override protected Boolean ReadRequest()
        {
            WriteOut("incoming");
            BTXMLDriveCom RobotData = (BTXMLDriveCom)DeserializeXML(nws);
            WriteOut("XML Object deserialized");
            WriteOut("Speed: "+RobotData.Speed);
            WriteOut("Acceleration: " + RobotData.Acceleration);

            if (RobotData.Command == Command.GO)
            {
                // send Go command to robot
                robot.runProgramInPath();
            }
            else
            {
                switch (RobotData.Command)
                {
                    case Command.LEFT:
                        robot.addTrack(new TrackArcLeft(RobotData.Radius, RobotData.Angle, RobotData.Speed, RobotData.Acceleration));
                        break;
                    case Command.RIGHT:
                        robot.addTrack(new TrackArcRight(RobotData.Radius,RobotData.Angle,RobotData.Speed, RobotData.Acceleration));
                        break;
                    case Command.LINE:
                        robot.addTrack(new TrackLine(RobotData.Length,RobotData.Speed,RobotData.Acceleration));
                        break;
                    case Command.TURN:
                        robot.addTrack(new TrackTurn(RobotData.Angle,RobotData.Speed,RobotData.Acceleration));
                        break;
                }
            }

            /*
            using (StreamWriter sw = new StreamWriter("protokoll.txt"))
            {
                sw.WriteLine("test");
            }
            */
            return true;
        }

        override protected void CreateResponse()
        {
            sw.WriteLine("Client Response: OK");
            sw.Flush();
        }

        private Object DeserializeXML(NetworkStream n)
        {
            //Write all data into MemoryStream
            // Da der Networkstream nicht geschlossen wird, müssen die
            // daten zuerst in einen MemoryStream geschrieben werden.
            MemoryStream ms = new MemoryStream();
            //Read first Byte in order to access the cl.DataAvailable property
            ms.WriteByte((byte)n.ReadByte());

            while (n.DataAvailable)
            {
                ms.WriteByte((byte)n.ReadByte());
            }

            ms.Position = 0;
            XmlSerializer xml = new XmlSerializer(typeof(BTXMLDriveCom));
            Object obj = xml.Deserialize(ms);

            return obj;
        }

        private void WriteOut(string s)
        {
            Console.WriteLine(s);
        }
    }
}

